package com.aube.activity;

import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.aube.BaseActivity;
import com.aube.R;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.SysUtil;
import com.huyn.baseframework.utils.UserUtil;

/**
 * Created by huyaonan on 15/12/26.
 */
public class WapActivity extends BaseActivity {

    public static final String WEB_URL = "WEB_URL";
    public static final String WEB_TITLE = "WEB_TITLE";
    public static final String WEB_POST = "WEB_POST";

    private WebView mWeb;

    private View mBack, mTopbar;
    private TextView mTitle;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ad);

        mBack = findViewById(R.id.topbar_back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if((mWeb != null && mWeb.canGoBack())) {
                    mWeb.goBack();
                    return;
                }
                finish();
            }
        });
        mTitle = (TextView) findViewById(R.id.topbar_title);

        String url = getIntent().getStringExtra(WEB_URL);
        String title = getIntent().getStringExtra(WEB_TITLE);
        boolean doPost = getIntent().getBooleanExtra(WEB_POST, false);
        if(StringUtils.isNotBlank(title))
            mTitle.setText(title);

        if(StringUtils.isBlank(url)) {
            finish();
            return;
        }

        mWeb = (WebView) findViewById(R.id.web_ad);
        mTopbar = findViewById(R.id.topbar);

        int statusHeight = SysUtil.getStatusHeight(this);
        if(statusHeight > 0)
            mTopbar.setPadding(0, statusHeight, 0, 0);

        WebSettings webSettings = mWeb.getSettings();

        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

        mWeb.setWebViewClient(new WebViewClient() {
            @Override
            public void onFormResubmission(WebView view, Message dontResend, Message resend) {
                dontResend.sendToTarget();
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            }
        });
        mWeb.setWebChromeClient(new WebChromeClient());

        if(doPost) {
            mWeb.postUrl(url, appendMemberArgs().getBytes());
        } else {
            mWeb.loadUrl(url);
        }
    }

    /**
     * 传递给H5登录信息
     * @return
     */
    private String appendMemberArgs() {
        return "userid=" + UserUtil.getMemberId() + "&memberToken=" + UserUtil.getMemberEncode();
    }

    public void finish() {
        super.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            if((mWeb != null && mWeb.canGoBack())) {
                mWeb.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
